package com.taptech.doufu.db.userlog;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.bean.UserLogModel;
import com.taptech.doufu.services.personalcenter.AccountService;

import java.util.ArrayList;
import java.util.List;

public class UserLogDBUtil {

    private UserLogDBHelper dbHelper;

    private UserLogDBUtil() {
        dbHelper = new UserLogDBHelper(WeMediaApplication.applicationContext);
    }

    //内部类实现
    private static class SingleInner{
        private static final UserLogDBUtil innerInstance = new UserLogDBUtil();
    }

    public static UserLogDBUtil getInstance() {
        return SingleInner.innerInstance;
    }

    private SQLiteDatabase getWritableDatabase() {
        if (dbHelper == null) {
            dbHelper = new UserLogDBHelper(WeMediaApplication.applicationContext);
        }
        return dbHelper.getWritableDatabase();
    }

    private SQLiteDatabase getReadableDatabase() {
        if (dbHelper == null) {
            dbHelper = new UserLogDBHelper(WeMediaApplication.applicationContext);
        }
        return dbHelper.getReadableDatabase();
    }

    public long insertLog(UserLogModel model) {
        if (model == null) {
            return 0;
        }
        synchronized (UserLogDBUtil.class) {
            if (dbHelper == null) {
                dbHelper = new UserLogDBHelper(WeMediaApplication.applicationContext);
            }
            SQLiteDatabase database = getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("novel_id",model.getNovelID());
            values.put("click_place",model.getClickPlace());
            values.put("event_name",model.getEventName());
            values.put("pre_page",model.getPrePage());
            values.put("cur_page",model.getCurPage());
            values.put("created_time",model.getTime());
            values.put("read_time",model.getReadTime());
            values.put("keyword",model.getKeyword());
            values.put("uid",AccountService.getInstance().getUserUid());
            long id = database.insert(UserLogDBHelper.TABLE_NAME, null, values);
            database.close();
            return id;
        }
    }


    public List<UserLogModel> getList(int size) {
        String sql = "SELECT * FROM "+UserLogDBHelper.TABLE_NAME +" LIMIT "+size;
        return getList(sql);
    }

    private List<UserLogModel> getList(String sql){
        synchronized (UserLogDBUtil.class) {
            List<UserLogModel> list = new ArrayList<>();
            SQLiteDatabase database = getReadableDatabase();
            Cursor cursor = database.rawQuery(sql, null);
            while (cursor.moveToNext()) {
                UserLogModel model = new UserLogModel();
                model.setDbID(cursor.getLong(cursor.getColumnIndex("_ID")));
                model.setEventName(cursor.getString(cursor.getColumnIndex("event_name")));
                model.setCurPage(cursor.getString(cursor.getColumnIndex("cur_page")));
                model.setPrePage(cursor.getString(cursor.getColumnIndex("pre_page")));
                model.setTime(cursor.getLong(cursor.getColumnIndex("created_time")));
                model.setNovelID(cursor.getString(cursor.getColumnIndex("novel_id")));
                model.setClickPlace(cursor.getString(cursor.getColumnIndex("click_place")));
                model.setReadTime(cursor.getLong(cursor.getColumnIndex("read_time")));
                model.setKeyword(cursor.getString(cursor.getColumnIndex("keyword")));
                model.setUid(cursor.getString(cursor.getColumnIndex("uid")));
                list.add(model);
            }
            cursor.close();
            database.close();
            return list;
        }
    }


    public void delete(List<UserLogModel> list) {
        synchronized (UserLogDBUtil.class) {
            SQLiteDatabase database = getWritableDatabase();
            for (UserLogModel modal:list) {
                if (modal != null && modal.getDbID() > 0) {
                    database.execSQL("DELETE FROM "+UserLogDBHelper.TABLE_NAME+" WHERE _ID = ?",
                            new Object[]{modal.getDbID()});
                }
            }
            database.close();
        }
    }




}
